// miniprogram/pages/chart/covid-19/covid-19.js
const covid_db = wx.cloud.database()

Page({
  data: {
    // 全国数据
    nationIndex: 0,
    nationChart: [{
      id: 'covid_case_new',
      covidFlag: 0
    }, {
      id: 'covid_case_now',
      covidFlag: 1
    }, {
      id: 'covid_num_cure_death',
      covidFlag: 2
    }],
    nationBtns: [
      ['新增疑似', '新增确诊'],
      ['现存疑似', '现存确诊'],
      ['死亡/治愈']
    ],
    //湖北数据
    bhIndex: 0,
    hbChart: [{
      id: 'covid_case_new_hb',
      covidFlag: 3
    }, {
      id: 'covid_case_now_hb',
      covidFlag: 4
    }],
    hubeiBtns: [
      ['湖北/非湖北', '新增确诊对比'],
      ['湖北/非湖北', '现存确诊对比']
    ]
  },
  onLoad: function (options) {
    this.setData({
      onling: getApp().globalData.onling
    })
    if (this.data.onling)
      return
    wx.setNavigationBarTitle({
      title: '疫情动态',
    })
    this.getCovidData()
  },
  onShareAppMessage: function () {

  },
  getCovidData() {
    wx.cloud.callFunction({
      name: "database",
      data: {
        ctName: 'CovidVirus'
      }
    }).then(res => {
      console.log(res)
      var covidData = res.result.data
      this.initCovidChart(covidData)
    }).catch(err => {
      console.error(err)
    })
  },
  initCovidChart(covidData) {
    var chartIds = ['#covid_case_new', '#covid_case_now', '#covid_num_cure_death', '#covid_case_new_hb', '#covid_case_now_hb']
    for (let i = 0; i < chartIds.length; i++) {
      // console.log(chartIds[i])
      var chartComponent = this.selectComponent(chartIds[i]);
      // console.log(chartComponent)
      chartComponent.initLineChart(covidData)
    }
  },
  nationClick(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    this.setData({
      nationIndex: index
    })
  },
  hubeiClick(e) {
    var index = e.currentTarget.dataset.index
    this.setData({
      bhIndex: index
    })
  },
  stopTouchMove() {
    return false;
  }
})